package com.zhku.onlineshop.Servlet;

import com.zhku.onlineshop.Pojo.Product;
import com.zhku.onlineshop.Pojo.Result;
import com.zhku.onlineshop.Service.LogginService;
import com.zhku.onlineshop.Service.ProductService;
import com.zhku.onlineshop.dto.ProductRating;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Slf4j
@RestController
public class ProductServlet {
    @Autowired
    private ProductService productservice;

    @GetMapping("/GetProductsList")
    public Result getAllProduct(){
        log.info("获取所有商品信息");

        List<Product> product1 = productservice.GetProductsList();
        if (product1 !=null)
            return Result.success(product1);
        else
            return Result.error("获取商品列表失败");
    }
    //店铺添加商品
    @PostMapping("/AddProduct")
    public Result AddProduct(
            @RequestParam("shop_img") MultipartFile shop_img,
            @RequestParam("shop_name") String shop_name,
            @RequestParam("shop_price") Double shop_price,
            @RequestParam("shop_description") String shop_description,
            @RequestParam("shop_id") Integer shop_id) throws IOException {

        byte[] shop_img1 = shop_img.getBytes();
        Product product = new Product();
        product.setShop_name(shop_name);
        product.setShop_description(shop_description);
        product.setShop_price(shop_price);
        product.setShop_img(shop_img1);
        product.setShop_id(shop_id);
        log.info("店铺添加商品");
        if(productservice.AddProduct(product))
            return Result.success();
            else
                return Result.error("添加商品失败");
    }

    //店铺查询商品
    @PostMapping("/GetProductsListById")
    public Result getProductById(Integer id){
        log.info("获取商家商品信息");
        List<Product> product = productservice.GetProductById(id);
        if (product !=null)
            return Result.success(product);
        else
            return Result.error("获取商品列表失败");
    }

    //店铺删除商品
    @PostMapping("/DelProductById")
    public Result DelProductById(Integer id){
        log.info("删除商品");
        if(productservice.DelProductById(id) )
            return Result.success();
        else
            return Result.error("删除失败");
    }
    //店铺修改商品
    @PostMapping("/UpdProduct")
    public Result UpdProduct(@RequestBody Product product){
        log.info("修改商品");
        if(productservice.UpdProduct(product))
            return Result.success();
        else
            return Result.error("修改失败");
    }
    //根据商品id获取评价表
    @GetMapping("/GetRattingById")
    public Result GetRattingById(Integer product_id){
        log.info("获取商品id为"+product_id+"的评价列表");
        List<ProductRating> productRatings = productservice.GetRattingById(product_id);
        if (productRatings != null)
            return Result.success(productRatings);
        else
            return  Result.error("获取评价列表失败");
    }
    //根据商品id添加评价
    @PostMapping("/AddRatting")
    public Result AddRatting(@RequestBody ProductRating productRating){
        log.info("添加评价");
        if(productservice.AddRatting(productRating))
            return Result.success();
        else
            return Result.error("评价添加失败");
    }
    @GetMapping("/SelTop")
    public Result SelTop(){
        log.info("查询排行前十的数据");
        return  Result.success(productservice.SelProduct());

    }
}
